package cn.iocoder.yudao.module.system.service.qnaApplication;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.qnaApplication.vo.QnaApplicationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.qnaApplication.vo.QnaApplicationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.qnaApplication.QnaApplicationDO;
import cn.iocoder.yudao.module.system.dal.mysql.qnaApplication.QnaApplicationMapper;
import cn.iocoder.yudao.module.system.enums.QnaApplicationEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getPortalLoginUserId;

/**
 * 需求申请 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class QnaApplicationServiceImpl implements QnaApplicationService {

    @Resource
    private QnaApplicationMapper qnaApplicationMapper;

    @Override
    public Integer createApplication(QnaApplicationSaveReqVO createReqVO) {
        // 插入
        QnaApplicationDO application = BeanUtils.toBean(createReqVO, QnaApplicationDO.class);
        application.setStatus(QnaApplicationEnum.Status.UNREPLIED.getStatus());
        application.setCreateTime(LocalDateTime.now());
        application.setCreateId(String.valueOf(getPortalLoginUserId()));
        qnaApplicationMapper.insert(application);
        // 返回
        return application.getId();
    }


    @Override
    public void updateApplication(QnaApplicationSaveReqVO updateReqVO) {
        // 更新
        QnaApplicationDO updateObj = BeanUtils.toBean(updateReqVO, QnaApplicationDO.class);
        qnaApplicationMapper.updateById(updateObj);
    }

    @Override
    public void deleteApplication(Integer id) {
        // 删除
        qnaApplicationMapper.deleteById(id);
    }

    @Override
        public void deleteApplicationListByIds(List<Integer> ids) {
        // 删除
        qnaApplicationMapper.deleteByIds(ids);
        }



    @Override
    public QnaApplicationDO getApplication(Integer id) {
        return qnaApplicationMapper.selectById(id);
    }

    @Override
    public PageResult<QnaApplicationDO> getApplicationPage(QnaApplicationPageReqVO pageReqVO) {
        return qnaApplicationMapper.selectPage(pageReqVO);
    }

    @Override
    public void reply(QnaApplicationSaveReqVO updateReqVO) {
        QnaApplicationDO updateObj = BeanUtils.toBean(updateReqVO, QnaApplicationDO.class);
        updateObj.setStatus(QnaApplicationEnum.Status.REPLIED.getStatus());
        updateObj.setReplyId(String.valueOf(getLoginUserId()));
        updateObj.setReplyTime(LocalDateTime.now());
        qnaApplicationMapper.updateById(updateObj);
    }

}